<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <div class="heard">
          <el-button type="primary" size="small" @click="dialogTableVisible = true">添加页面</el-button>
          <div class="input">
            <el-input v-model="input" placeholder="请输入内容" class="input-with-select">
              <el-button slot="append" @click="searchFn">筛选</el-button>
            </el-input>
          </div>
        </div>
        <el-table
          :data="pageList"
          border
          style="width: 100%; margin: 30px 0"
        >
          <el-table-column
            prop="title"
            label="标题"
          />
          <el-table-column
            prop="content"
            label="内容"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="操作"
          >
            <template v-slot="{row}">
              <el-button type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delFn(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          align="center"
          :total="total"
          :current-page="page"
          :page-size="limit"
          layout="prev, pager, next, total"
          @current-change="handleCurrentChange"
        />
      </el-card>

      <el-dialog :title="form.id ? '编辑页面' :'添加页面'" :visible.sync="dialogTableVisible" @close="closeFn">
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <vue-editor v-model="form.content" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeFn">取 消</el-button>
          <el-button type="primary" @click="firmFn(form.id)">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPageList, getPageCount, addPage, onePage, editPage, delPage } from '@/api/page'
import { VueEditor } from 'vue2-editor'
export default {
  components: { VueEditor },
  data() {
    return {
      dialogTableVisible: false,
      form: {
        content: '',
        title: ''
      },
      input: '',
      pageList: [],
      total: 0,
      page: 1,
      limit: 15,
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPageList()
    this.getPageCount()
  },
  methods: {
    // 获取页面列表
    async getPageList(a, b, c) {
      this.pageList = await getPageList(a = this.limit, b = this.limit * (this.page - 1), c = this.input)

      this.pageList.forEach(v => {
        /* 去除富文本中的html标签 */
        v.content = v.content.replace(/<.+?>/g, '')
        /* 去除&nbsp; */
        v.content = v.content.replace(/&nbsp;/ig, '')
        /* 去除空格 */
        v.content = v.content.replace(/\s/ig, '')
      })
    },
    // 获取页面数量
    async getPageCount(a) {
      this.total = await getPageCount(a = this.input)
    },
    // 点击切换页面回调
    handleCurrentChange(page) {
      this.page = page
      this.getPageList(this.limit, this.page)
      this.getPageCount()
    },
    // 确定按钮回调
    async firmFn(id) {
      console.log(id)
      this.$refs.form.validate()
      if (id) {
        await editPage(this.form)
        this.$message.success('编辑成功')
      } else {
        await addPage(this.form)
        this.$message.success('添加成功')
      }
      this.getPageList()
      this.getPageCount()
      this.closeFn()
    },
    // 编辑按钮回调
    async editFn(id) {
      this.dialogTableVisible = true
      const { title, content } = await onePage(id)
      this.form = {
        title,
        content,
        id
      }
    },
    // 关闭对话框回调
    closeFn() {
      this.dialogTableVisible = false
      this.form = {
        title: '',
        content: ''
      }
    },
    // 删除回调
    delFn(row) {
      this.$confirm(`是否删除${row.title}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPage(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getPageList()
        this.getPageCount()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    searchFn() {
      this.input = this.input.trim()
      this.getPageList(this.limit, this.page, this.input)
      this.getPageCount(this.input)
    }
  }
}
</script>

<style scoped>
  .heard{
    padding-bottom: 30px;
    border-bottom: 1px solid #eee;
  }
  .el-card{
    padding: 10px;
  }
  .input{
    float:right;
    width: 60%;
  }

</style>
